/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Cali.java
 *
 * Created on 10-dic-2009, 18:52:06
 */
package Vista;

import Controlador.Controlador;
import Modelo.Alertas;
import Modelo.GeomantesDatos;
import Modelo.Idiomas.LocaleManager;
import Utils.Ficheros.FicheroDatos;
import com.chaeron.GPS.exception.GPSException;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author Administrador
 */
public class GUI_Calibracion extends javax.swing.JDialog {

    private static Controlador controlador;
    private static JPanel mapa;
    private int puntoACalibrar; // 1 = Punto1   |  2 = Punto2
    private double lat1, lat2, long1, long2;
    private int pixelX1, pixelX2, pixelY1, pixelY2;
    private Cursor defaultCursor;
    private boolean punto1 = false;
    private boolean punto2 = false;
    private double separacionMinima = 0.0001;
    private int ndatum;
    private String datum;

    /** Creates new form Cali */
    public GUI_Calibracion(JFrame parent) {
        super(parent, true);
        controlador = Inicio.getControlador();
        initComponents();
        mapa = PMapa;
        controlador.setDisplayMapa(mapa);
        cambioIdioma();
        activarLatLong(true);
        puntoACalibrar = 0;
        //parent.setVisible(false);
        centrarAplicacion();
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setVisible(true);
        defaultCursor = PMapa.getCursor();
    }

    public static JPanel getDisplayMapa() {
        return mapa;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        BGModoCalibracion = new javax.swing.ButtonGroup();
        JPCoordenadas = new javax.swing.JPanel();
        TXTLong1 = new javax.swing.JTextField();
        TXTLat1 = new javax.swing.JTextField();
        LLong = new javax.swing.JLabel();
        LLat = new javax.swing.JLabel();
        BPunto1 = new javax.swing.JButton();
        BPunto2 = new javax.swing.JButton();
        TXTLat2 = new javax.swing.JTextField();
        TXTLong2 = new javax.swing.JTextField();
        BDatosGps1 = new javax.swing.JButton();
        BDatosGps2 = new javax.swing.JButton();
        DPMapa = new javax.swing.JDesktopPane();
        PMapa = new javax.swing.JPanel();
        JPPixel = new javax.swing.JPanel();
        LabelX = new javax.swing.JLabel();
        LabelY = new javax.swing.JLabel();
        TXTPixelX = new javax.swing.JTextField();
        TXTPixelY = new javax.swing.JTextField();
        JDPCargarMapa = new javax.swing.JDesktopPane();
        BCargarMapa = new javax.swing.JButton();
        JDPCalibrar = new javax.swing.JDesktopPane();
        BCalibrar = new javax.swing.JButton();
        JDPSalir = new javax.swing.JDesktopPane();
        BSalir = new javax.swing.JButton();
        Datum = new javax.swing.JPanel();
        DatumList = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("");
        setResizable(false);

        JPCoordenadas.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(102, 153, 255), 3, true), "Coordenadas", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Arial", 1, 14))); // NOI18N

        TXTLong1.setForeground(java.awt.Color.blue);
        TXTLong1.setVisible(false);
        TXTLong1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TXTLong1ActionPerformed(evt);
            }
        });
        TXTLong1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                TXTLong1KeyTyped(evt);
            }
        });

        TXTLat1.setForeground(java.awt.Color.blue);
        TXTLat1.setVisible(false);
        TXTLat1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TXTLat1ActionPerformed(evt);
            }
        });
        TXTLat1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                TXTLat1PropertyChange(evt);
            }
        });

        LLong.setText("Longitud:");
        LLong.setVisible(false);

        LLat.setText("Latitud:");
        LLat.setVisible(false);

        BPunto1.setText("Punto 1");
        BPunto1.setEnabled(false);
        BPunto1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BPunto1ActionPerformed(evt);
            }
        });

        BPunto2.setText("Punto 2");
        BPunto2.setEnabled(false);
        BPunto2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BPunto2ActionPerformed(evt);
            }
        });

        TXTLat2.setForeground(java.awt.Color.blue);
        TXTLat1.setVisible(false);
        TXTLat2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                TXTLat2KeyTyped(evt);
            }
        });

        TXTLong2.setForeground(java.awt.Color.blue);
        TXTLong1.setVisible(false);
        TXTLong2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                TXTLong2KeyTyped(evt);
            }
        });

        BDatosGps1.setText("Datos GPS");
        BDatosGps1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BDatosGps1ActionPerformed(evt);
            }
        });

        BDatosGps2.setText("Datos GPS");
        BDatosGps2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BDatosGps2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout JPCoordenadasLayout = new javax.swing.GroupLayout(JPCoordenadas);
        JPCoordenadas.setLayout(JPCoordenadasLayout);
        JPCoordenadasLayout.setHorizontalGroup(
            JPCoordenadasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JPCoordenadasLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(JPCoordenadasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(LLong)
                    .addComponent(LLat, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(JPCoordenadasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(BDatosGps1, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
                    .addGroup(JPCoordenadasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(BPunto1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(TXTLong1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
                        .addComponent(TXTLat1, javax.swing.GroupLayout.Alignment.LEADING)))
                .addGap(20, 20, 20)
                .addGroup(JPCoordenadasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(BPunto2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(TXTLat2)
                    .addComponent(TXTLong2)
                    .addComponent(BDatosGps2, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        JPCoordenadasLayout.setVerticalGroup(
            JPCoordenadasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JPCoordenadasLayout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(JPCoordenadasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(JPCoordenadasLayout.createSequentialGroup()
                        .addComponent(BPunto1)
                        .addGap(18, 18, 18)
                        .addComponent(TXTLat1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(TXTLong1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(JPCoordenadasLayout.createSequentialGroup()
                        .addComponent(BPunto2)
                        .addGap(18, 18, 18)
                        .addGroup(JPCoordenadasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(TXTLat2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(LLat))
                        .addGap(18, 18, 18)
                        .addGroup(JPCoordenadasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(TXTLong2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(LLong))
                        .addGap(15, 15, 15)
                        .addGroup(JPCoordenadasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(BDatosGps2, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(BDatosGps1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        DPMapa.setBackground(new java.awt.Color(235, 233, 237));
        DPMapa.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(102, 153, 255), 3, true), "Mapa", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Arial", 1, 14))); // NOI18N
        DPMapa.setForeground(new java.awt.Color(235, 233, 237));
        DPMapa.setPreferredSize(new java.awt.Dimension(650, 560));

        PMapa.setToolTipText("");
        PMapa.setName("Mapa"); // NOI18N
        PMapa.setPreferredSize(new java.awt.Dimension(633, 548));
        PMapa.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                PMapaMouseClicked(evt);
            }
        });
        PMapa.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                PMapaMouseMoved(evt);
            }
        });

        javax.swing.GroupLayout PMapaLayout = new javax.swing.GroupLayout(PMapa);
        PMapa.setLayout(PMapaLayout);
        PMapaLayout.setHorizontalGroup(
            PMapaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 633, Short.MAX_VALUE)
        );
        PMapaLayout.setVerticalGroup(
            PMapaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 548, Short.MAX_VALUE)
        );

        PMapa.setBounds(10, 20, 633, 548);
        DPMapa.add(PMapa, javax.swing.JLayeredPane.DEFAULT_LAYER);

        JPPixel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(102, 153, 255), 3, true), "Pixel X,Y", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Arial", 1, 14))); // NOI18N

        LabelX.setText("X:");

        LabelY.setText("Y:");

        TXTPixelX.setEditable(false);
        TXTPixelX.setVisible(false);

        TXTPixelY.setEditable(false);
        TXTPixelY.setVisible(false);

        javax.swing.GroupLayout JPPixelLayout = new javax.swing.GroupLayout(JPPixel);
        JPPixel.setLayout(JPPixelLayout);
        JPPixelLayout.setHorizontalGroup(
            JPPixelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JPPixelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(LabelX, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TXTPixelX, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(118, 118, 118)
                .addComponent(LabelY, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(TXTPixelY, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(35, 35, 35))
        );
        JPPixelLayout.setVerticalGroup(
            JPPixelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JPPixelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(JPPixelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LabelX)
                    .addComponent(TXTPixelX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(TXTPixelY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(LabelY))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        JDPCargarMapa.setBackground(new java.awt.Color(235, 233, 237));
        JDPCargarMapa.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(102, 153, 255), 3, true), "", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Arial", 1, 14))); // NOI18N
        JDPCargarMapa.setForeground(new java.awt.Color(235, 233, 237));

        BCargarMapa.setText("Cargar Imagen");
        BCargarMapa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCargarMapaActionPerformed(evt);
            }
        });
        BCargarMapa.setBounds(87, 10, 170, 33);
        JDPCargarMapa.add(BCargarMapa, javax.swing.JLayeredPane.DEFAULT_LAYER);

        JDPCalibrar.setBackground(new java.awt.Color(235, 233, 237));
        JDPCalibrar.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(102, 153, 255), 3, true), "", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Arial", 1, 14))); // NOI18N
        JDPCalibrar.setForeground(new java.awt.Color(235, 233, 237));

        BCalibrar.setFont(new java.awt.Font("Tahoma", 0, 18));
        BCalibrar.setForeground(java.awt.Color.red);
        BCalibrar.setText("CALIBRAR");
        BCalibrar.setEnabled(false);
        BCalibrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCalibrarActionPerformed(evt);
            }
        });
        BCalibrar.setBounds(10, 10, 141, 35);
        JDPCalibrar.add(BCalibrar, javax.swing.JLayeredPane.DEFAULT_LAYER);

        JDPSalir.setBackground(new java.awt.Color(235, 233, 237));
        JDPSalir.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(102, 153, 255), 3, true), "", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Arial", 1, 14))); // NOI18N
        JDPSalir.setForeground(new java.awt.Color(235, 233, 237));

        BSalir.setFont(new java.awt.Font("Tahoma", 0, 18));
        BSalir.setText("Salir");
        BSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BSalirActionPerformed(evt);
            }
        });
        BSalir.setBounds(10, 10, 141, 35);
        JDPSalir.add(BSalir, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Datum.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(102, 153, 255), 3, true), "Datum", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Arial", 1, 14))); // NOI18N

        DatumList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Placeholder", "Airy", "Australian National", "Bessel 1841", "Bessel 1841 (Nambia)", "Clarke 1866", "Clarke 1880", "Everest", "Fischer 1960 (Mercury)", "Fischer 1968", "GRS 1967", "GRS 1980", "Helmert 1906", "Hough", "International", "Krassovsky", "Modified Airy", "Modified Everest", "Modified Fischer 1960", "South American 1969", "WGS 60", "WGS 66", "WGS 72", "WGS 84", "European 1950" }));
        DatumList.setSelectedIndex(23);
        DatumList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DatumListActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout DatumLayout = new javax.swing.GroupLayout(Datum);
        Datum.setLayout(DatumLayout);
        DatumLayout.setHorizontalGroup(
            DatumLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, DatumLayout.createSequentialGroup()
                .addContainerGap(97, Short.MAX_VALUE)
                .addComponent(DatumList, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(88, 88, 88))
        );
        DatumLayout.setVerticalGroup(
            DatumLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, DatumLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(DatumList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(DPMapa, javax.swing.GroupLayout.PREFERRED_SIZE, 653, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(JPCoordenadas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(JPPixel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(JDPCalibrar, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(JDPSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(JDPCargarMapa, javax.swing.GroupLayout.DEFAULT_SIZE, 343, Short.MAX_VALUE)
                    .addComponent(Datum, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(DPMapa, javax.swing.GroupLayout.PREFERRED_SIZE, 578, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(JDPCargarMapa, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(JPPixel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(JPCoordenadas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(83, 83, 83)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addGap(66, 66, 66)
                                    .addComponent(JDPCalibrar, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(66, 66, 66)
                                    .addComponent(JDPSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(391, 391, 391)
                        .addComponent(Datum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void PMapaMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_PMapaMouseMoved
        TXTPixelX.setText(String.valueOf(evt.getX()));
        TXTPixelY.setText(String.valueOf(evt.getY()));
    }//GEN-LAST:event_PMapaMouseMoved

    private void PMapaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_PMapaMouseClicked
        switch (puntoACalibrar) {
            case 1: {
                pixelX1 = evt.getX();
                pixelY1 = evt.getY();
                controlador.getCalibracion().setPixelX1(pixelX1);
                controlador.getCalibracion().setPixelY1(pixelY1);
                controlador.getCalibracion().paintBorder(null, PMapa.getGraphics(), 0, 0, 0, 0);
                punto1 = true;
                if (!TXTLat1.getText().equals("") && !TXTLong1.getText().equals("") &&
                        !TXTLat2.getText().equals("") && !TXTLong2.getText().equals("") &&
                        punto1 && punto2) {
                    activarCalibrar(true);
                } else {
                    activarCalibrar(false);
                }
                break;
            }
            case 2: {
                pixelX2 = evt.getX();
                pixelY2 = evt.getY();
                controlador.getCalibracion().setPixelX2(pixelX2);
                controlador.getCalibracion().setPixelY2(pixelY2);
                controlador.getCalibracion().paintBorder(null, PMapa.getGraphics(), 0, 0, 0, 0);
                punto2 = true;
                if (!TXTLat1.getText().equals("") && !TXTLong1.getText().equals("") &&
                        !TXTLat2.getText().equals("") && !TXTLong2.getText().equals("") &&
                        punto1 && punto2) {
                    activarCalibrar(true);
                } else {
                    activarCalibrar(false);
                }

                break;
            }
        }
    }//GEN-LAST:event_PMapaMouseClicked

    private void BCargarMapaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCargarMapaActionPerformed
        controlador.calibracion(this);
        activarPuntos(true);
    }//GEN-LAST:event_BCargarMapaActionPerformed

    private void BCalibrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCalibrarActionPerformed
        if (okTxt(TXTLat1, TXTLong1, TXTLat2, TXTLong2)) {
            lat1 = Double.valueOf(TXTLat1.getText());
            long1 = Double.valueOf(TXTLong1.getText());
            ndatum = DatumList.getSelectedIndex();
            datum = (String)DatumList.getSelectedItem();
            controlador.getCalibracion().setPixelX1(pixelX1);
            controlador.getCalibracion().setPixelY1(pixelY1);
            controlador.getCalibracion().paintBorder(null, PMapa.getGraphics(), 0, 0, 0, 0);
            lat2 = Double.valueOf(TXTLat2.getText());
            long2 = Double.valueOf(TXTLong2.getText());
            controlador.getCalibracion().setPixelX2(pixelX2);
            controlador.getCalibracion().setPixelY2(pixelY2);
            controlador.getCalibracion().paintBorder(null, PMapa.getGraphics(), 0, 0, 0, 0);
            if (okDistancia(lat1, lat2, long1, long2)) {
                if (okRango(lat1, lat2, long1, long2)) {
                    controlador.getCalibracion().setGradosX1(long1);
                    controlador.getCalibracion().setGradosX2(long2);
                    controlador.getCalibracion().setGradosY1(lat1);
                    controlador.getCalibracion().setGradosY2(lat2);
                    controlador.getCalibracion().setNDatum(ndatum);
                    controlador.getCalibracion().setDatum(datum);
                    int seleccion = controlador.getCalibracion().calibrar();
                    if (seleccion == 0) {
                        Alertas.exitoCalibracion(this);
                    }
                    //reiniciar();
                } else {
                    Alertas.falloCalibracionRango(this); //Rango
                }
            }
            else {
                Alertas.falloCalibracionDistancia(this);
            }
        } else {
            Alertas.falloCalibracionTXT(this);
        }
        PMapa.setCursor(Cursor.getDefaultCursor());
    }//GEN-LAST:event_BCalibrarActionPerformed

    private void BPunto1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BPunto1ActionPerformed
        //Cambiamos el cursor
        Image imagenCursor = Toolkit.getDefaultToolkit().getImage(Toolkit.getDefaultToolkit().getClass().getResource("/Archivos/Imagenes/marcador.png"));
        Point hotSpot = new Point(0, 0);
        Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(imagenCursor, hotSpot, "cursorMarcar");
        PMapa.setCursor(cursor);
        //---------------------------------------------------------------------
        activarLatLong(true);
        activarPixeles(true);
        BPunto2.setForeground(Color.BLACK);
        BPunto2.setFont(new Font("Tahoma", Font.PLAIN, 11));
        BPunto1.setForeground(Color.red);
        BPunto1.setFont(new Font("Tahoma", Font.BOLD, 12));
        puntoACalibrar = 1;
        if (!TXTLat1.getText().equals("") && !TXTLong1.getText().equals("") &&
                !TXTLat2.getText().equals("") && !TXTLong2.getText().equals("") &&
                punto1 && punto2) {
            activarCalibrar(true);
        } else {
            activarCalibrar(false);
        }

    }//GEN-LAST:event_BPunto1ActionPerformed

    private void BPunto2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BPunto2ActionPerformed
        activarLatLong(true);
        activarPixeles(true);
        BPunto1.setForeground(Color.BLACK);
        BPunto1.setFont(new Font("Tahoma", Font.PLAIN, 11));
        BPunto2.setForeground(Color.red);
        BPunto2.setFont(new Font("Tahoma", Font.BOLD, 12));
        puntoACalibrar = 2;
        if (!TXTLat1.getText().equals("") && !TXTLong1.getText().equals("") &&
                !TXTLat2.getText().equals("") && !TXTLong2.getText().equals("") &&
                punto1 && punto2) {
            activarCalibrar(true);
        } else {
            activarCalibrar(false);
        }

    }//GEN-LAST:event_BPunto2ActionPerformed

    private void BDatosGps2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BDatosGps2ActionPerformed
        try {
            GeomantesDatos gps = new GeomantesDatos();
            gps.initGPS();
            gps.pollGPS();
            FicheroDatos pos1 = gps.getCoordenada();
            lat2 = pos1.getLatitud();
            long2 = pos1.getLongitud();
            TXTLat2.setText(String.valueOf(lat2));
            TXTLong2.setText(String.valueOf(long2));
            gps.disconnectGPS();
        } catch (GPSException ex) {
            Logger.getLogger(GUI_Calibracion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_BDatosGps2ActionPerformed

    private void BSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BSalirActionPerformed
        dispose();
    }//GEN-LAST:event_BSalirActionPerformed

    private void BDatosGps1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BDatosGps1ActionPerformed
        try {
            // TODO add your handling code here:
            GeomantesDatos gps = new GeomantesDatos();
            gps.initGPS();
            gps.pollGPS();
            FicheroDatos pos1 = gps.getCoordenada();
            lat1 = pos1.getLatitud();
            long1 = pos1.getLongitud();
            TXTLat1.setText(String.valueOf(lat1));
            TXTLong1.setText(String.valueOf(long1));
            gps.disconnectGPS();
        } catch (GPSException ex) {
            Logger.getLogger(GUI_Calibracion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_BDatosGps1ActionPerformed

    private void TXTLat1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TXTLat1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TXTLat1ActionPerformed

    private void TXTLat1PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_TXTLat1PropertyChange
        // TODO add your handling code here:
    }//GEN-LAST:event_TXTLat1PropertyChange

    private void TXTLat2KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TXTLat2KeyTyped
        // TODO add your handling code here:
        if (TXTLat1.getText().equals("") || TXTLat2.getText().equals("") ||
                TXTLong1.getText().equals("") || TXTLong2.getText().equals("")) {
            activarCalibrar(false);
        } else {
            if (!TXTLat1.getText().equals("") && !TXTLong1.getText().equals("") &&
                    !TXTLat2.getText().equals("") && !TXTLong2.getText().equals("") &&
                    punto1 && punto2) {
                activarCalibrar(true);
            }

        }
    }//GEN-LAST:event_TXTLat2KeyTyped

    private void TXTLong1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TXTLong1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TXTLong1ActionPerformed

    private void TXTLong1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TXTLong1KeyTyped
        if (TXTLat1.getText().equals("") || TXTLat2.getText().equals("") ||
                TXTLong1.getText().equals("") || TXTLong2.getText().equals("")) {
            activarCalibrar(false);
        } else {
            if (!TXTLat1.getText().equals("") && !TXTLong1.getText().equals("") &&
                    !TXTLat2.getText().equals("") && !TXTLong2.getText().equals("") &&
                    punto1 && punto2) {
                activarCalibrar(true);
            }

        }
    }//GEN-LAST:event_TXTLong1KeyTyped

    private void TXTLong2KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TXTLong2KeyTyped
        if (TXTLat1.getText().equals("") || TXTLat2.getText().equals("") ||
                TXTLong1.getText().equals("") || TXTLong2.getText().equals("")) {
            activarCalibrar(false);
        } else {
            if (!TXTLat1.getText().equals("") && !TXTLong1.getText().equals("") &&
                    !TXTLat2.getText().equals("") && !TXTLong2.getText().equals("") &&
                    punto1 && punto2) {
                activarCalibrar(true);
            }

        }
    }//GEN-LAST:event_TXTLong2KeyTyped

    private void DatumListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DatumListActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_DatumListActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BCalibrar;
    private javax.swing.JButton BCargarMapa;
    private javax.swing.JButton BDatosGps1;
    private javax.swing.JButton BDatosGps2;
    private javax.swing.ButtonGroup BGModoCalibracion;
    private javax.swing.JButton BPunto1;
    private javax.swing.JButton BPunto2;
    private javax.swing.JButton BSalir;
    private javax.swing.JDesktopPane DPMapa;
    private javax.swing.JPanel Datum;
    private javax.swing.JComboBox DatumList;
    private javax.swing.JDesktopPane JDPCalibrar;
    private javax.swing.JDesktopPane JDPCargarMapa;
    private javax.swing.JDesktopPane JDPSalir;
    private javax.swing.JPanel JPCoordenadas;
    private javax.swing.JPanel JPPixel;
    private javax.swing.JLabel LLat;
    private javax.swing.JLabel LLong;
    private javax.swing.JLabel LabelX;
    private javax.swing.JLabel LabelY;
    private javax.swing.JPanel PMapa;
    private javax.swing.JTextField TXTLat1;
    private javax.swing.JTextField TXTLat2;
    private javax.swing.JTextField TXTLong1;
    private javax.swing.JTextField TXTLong2;
    private javax.swing.JTextField TXTPixelX;
    private javax.swing.JTextField TXTPixelY;
    // End of variables declaration//GEN-END:variables

    private void detectaCambioTexto(java.awt.event.TextEvent evt) {
    }

    private void centrarAplicacion() {
        int width = this.getWidth();
        int height = this.getHeight();
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screen.width - width) / 2;
        int y = (screen.height - height) / 2;
        setBounds(x, y, width, height);
    }

    private void activarLatLong(boolean t) {
        this.TXTLat1.setVisible(t);
        this.TXTLong1.setVisible(t);
        this.TXTLat2.setVisible(t);
        this.TXTLong2.setVisible(t);
        this.LLong.setVisible(t);
        this.LLat.setVisible(t);
    }

    private void activarPuntos(boolean t) {
        this.BPunto1.setEnabled(t);
        this.BPunto2.setEnabled(t);
    }

    private void activarPixeles(boolean t) {
        this.TXTPixelX.setVisible(t);
        this.TXTPixelY.setVisible(t);
    }

    private void activarCalibrar(boolean t) {
        this.BCalibrar.setEnabled(t);
    }

    private void reiniciar() {
        activarPuntos(false);
        activarPixeles(false);
        activarCalibrar(false);
        activarLatLong(false);
        controlador.getCalibracion().setPixelX1(-15);
        controlador.getCalibracion().setPixelY1(-15);
        controlador.getCalibracion().setPixelX2(-15);
        controlador.getCalibracion().setPixelY2(-15);
        PMapa.setBorder(null);
        PMapa.setCursor(defaultCursor);
    }

    private void cambioIdioma() {
        this.setTitle(LocaleManager.getMessage("calibracionTitulo"));
        this.BCalibrar.setText(LocaleManager.getMessage("calibracionCalibrar"));
        this.LLat.setText(LocaleManager.getMessage("calibracionLatitud"));
        this.LLong.setText(LocaleManager.getMessage("calibracionLongitud"));
        this.BCargarMapa.setText(LocaleManager.getMessage("calibracionCargar"));
        this.DPMapa.setName(LocaleManager.getMessage("calibracionMapa"));
        this.BSalir.setText(LocaleManager.getMessage("calibracionSalir"));
        this.BPunto1.setText(LocaleManager.getMessage("calibracionPunto1"));
        this.BPunto2.setText(LocaleManager.getMessage("calibracionPunto2"));
        this.BDatosGps1.setText(LocaleManager.getMessage("calibracionDatosGps"));
        this.BDatosGps2.setText(LocaleManager.getMessage("calibracionDatosGps"));
        this.JPCoordenadas.setBorder(
                javax.swing.BorderFactory.createTitledBorder(
                new javax.swing.border.LineBorder(
                new java.awt.Color(102, 153, 255), 3, true),
                LocaleManager.getMessage("calibracionCoordenadas"),
                javax.swing.border.TitledBorder.CENTER,
                javax.swing.border.TitledBorder.TOP,
                new java.awt.Font("Arial", 1, 14)));
        this.DPMapa.setBorder(
                javax.swing.BorderFactory.createTitledBorder(
                new javax.swing.border.LineBorder(
                new java.awt.Color(102, 153, 255), 3, true),
                LocaleManager.getMessage("calibracionMapa"),
                javax.swing.border.TitledBorder.CENTER,
                javax.swing.border.TitledBorder.TOP,
                new java.awt.Font("Arial", 1, 14)));
        this.JPPixel.setName(LocaleManager.getMessage("calibracionXY"));
    }

    private boolean okTxt(JTextField tLat1, JTextField tLong1, JTextField tLat2, JTextField tLong2) {
        return !tLat1.getText().equals("") &&
                !tLong1.getText().equals("") &&
                !tLat2.getText().equals("") &&
                !tLong2.getText().equals("");
    }

    private boolean okDistancia(double vLat1, double vLat2, double vLong1, double vLong2) {
        return (Math.abs(vLat1 - vLat2) > separacionMinima ||
                Math.abs(vLong1 - vLong2) > separacionMinima);
    }

    private boolean okRango(double vLat1, double vLat2, double vLong1, double vLong2) {
        return (vLat1 <= 90 && vLat1 >= -90) &&
                (vLat2 <= 90 && vLat2 >= -90) &&
                (vLong1 <= 180 && vLong1 >= -180) &&
                (vLong2 <= 180 && vLong2 >= -180);
    }
}
